package cn.acewill.wsh.daggerandroid.app

import android.app.Activity
import android.app.Application
import androidx.annotation.Nullable
import cn.acewill.wsh.daggerandroid.BuildConfig
import cn.acewill.wsh.daggerandroid.di.component.DaggerAppComponent
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import javax.inject.Inject

/**
 * 作者：<a href="http://www.cnblogs.com/baiqiantao">张勇</a><p>
 * 创建时间：2020/1/11 20:47 <p>
 * 描述：
 * @author zy
 */
class DaggerAndroidApplication : Application(), HasActivityInjector {
    @Inject
    lateinit var injector: DispatchingAndroidInjector<Activity>

    override fun activityInjector(): AndroidInjector<Activity> {
        return injector
    }

    override fun onCreate() {
        super.onCreate()
        initLogger()
        initInjecter()
    }

    private fun initInjecter() {
        DaggerAppComponent.create().inject(this)
    }

    private fun initLogger() {
        val pfs = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(true)
            .methodCount(0)
            .methodOffset(7)
            .tag("debuga")
            .build()
        Logger.addLogAdapter(object : AndroidLogAdapter(pfs) {
            override fun isLoggable(priority: Int, @Nullable tag: String?): Boolean {
                return BuildConfig.DEBUG
            }
        })
    }
}